package com.qaml.mes.device.domain.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.qaml.mes.device.api.EqpTypeConfigService;
import com.qaml.mes.device.dao.EqpCraftTypeDO;
import com.qaml.mes.device.dao.EqpTypeConfigDO;
import com.qaml.mes.device.domain.convert.EqpInfoConvert;
import com.qaml.mes.device.repository.ConfigRepository;
import com.qaml.mes.device.domain.convert.EqpTypeConfigConvert;
import com.qaml.mes.device.vo.eqp.EqpCraftConfigVO;
import com.qaml.mes.device.vo.eqp.EqpTypeConfigVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备类型表 服务实现类
 * </p>
 *
 * @author leikai
 * @since 2024-03-07
 */
@Service
public class EqpTypeConfigServiceImpl implements EqpTypeConfigService {

    @Resource
    private ConfigRepository configRepository;


    @Override
    public List<EqpTypeConfigVO> list() {
        List<EqpTypeConfigDO> doList = configRepository.list();
        return EqpTypeConfigConvert.INSTANCE.do2VoList(doList);
    }

    @Override
    public List<EqpCraftConfigVO> craftList() {
        List<EqpCraftTypeDO> eqpCraftTypeDOS = configRepository.craftList();
        return EqpTypeConfigConvert.INSTANCE.craftDo2Vos(eqpCraftTypeDOS);
    }

    @Override
    public Map<String, EqpCraftConfigVO> craftMap() {
        List<EqpCraftConfigVO> eqpCraftConfigVOS = craftList();
        if (!CollectionUtils.isEmpty(eqpCraftConfigVOS)) {
            return eqpCraftConfigVOS.stream().collect(Collectors.toMap(EqpCraftConfigVO::getCraftTypeCode, item -> item));
        }
        return new HashMap<>();
    }

    @Override
    public EqpCraftConfigVO craftByCraftTypeCode(String craftTypeCode) {
        EqpCraftTypeDO eqpTypeConfigDO = configRepository.craftByCraftTypeCode(craftTypeCode);
        return EqpTypeConfigConvert.INSTANCE.craftDo2Vo(eqpTypeConfigDO);
    }
}
